<template>

  <div class="header-container">
    <div class="header-left">
      <el-icon class="icon" size="20" @click="store.commit('collapseMenu')"><Fold /></el-icon>
      <ul class="flex-box">
        <li v-for="(item,index) in menuList" :key="item.path" class="tab flex-box" :class="{ selected: route.path===item.path}">
          <el-icon  size="12"><component :is="item.icon"></component></el-icon>
          <router-link class="text flex-box" :to="{path:item.path}">
            {{item.name}}
          </router-link>
          <el-icon  size="12" class="close" @click="closeTab(item,index)"><Close /></el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
        <el-dropdown @command="clickOut">
          <div class="el-dropdown-link flex-box">
            <el-avatar
                :src="userinfo.avatar"
            />
            <p class="user-name">{{userinfo.username}}</p>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">登出</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
    </div>
  </div>
</template>
<script setup>
import {computed, reactive} from 'vue'
import {useStore} from 'vuex'
import {useRoute,useRouter} from 'vue-router'
const userinfo=reactive(JSON.parse(localStorage.getItem('userInfo')))
const store = useStore()
const route = useRoute()
const router = useRouter()
const menuList = computed(() => store.state.menu.menuList)
const closeTab = (item, index) => {
  store.commit("closeMenu",item)
  //删除非当前页面
  if (route.path !== item.path) {
    return
  }
  const selectMenuData = menuList.value;

  // 删除的最后一项
  if (index === selectMenuData.length) {
    // 如果是最后一个，跳转至首页
    if (!selectMenuData.length) {
      router.push({
        path: "/dashboard",
      });
    } else {
      router.push({
        path: selectMenuData[index - 1].path,
      });
    }
  } else {
    router.push({
      path: selectMenuData[index].path,
    });
  }
}
const clickOut = (command) => {
  if (command === 'logout') {
    console.log('logout')
    localStorage.removeItem('userInfo')
    localStorage.removeItem('token')
    localStorage.removeItem('r_vuex')
    menuList.value = []
    router.push('/login')
  }
}
</script>
<style lang="less" scoped>
.flex-box{
  display: flex;
  align-items: center;
  height: 100%;
}
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-color: #fff;
  padding-right: 25px;
  .header-left {
    display: flex;
    align-items: center;
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f5;
      cursor: pointer;
    }
    .tab{
      padding: 0 10px;
      height: 100%;
      .text{
        margin: 0 5px;
      }
      .close{
        visibility: hidden;
      }
      &.selected{
        a{
          color:#409eff;
        }
        i{
          color:#409eff;
        }
        background-color: #f5f5f5;
      }
    }
    .tab:hover {
      background-color: #f5f5f5;

      .close {
        visibility: inherit;
        cursor: pointer;
        color: black;
      }
    }
  }
  .header-right {
    display: flex;
    align-items: center;
    height: 100%;
    .user-name {
      margin-left: 10px;
    }
  }
  a{
    height: 100%;
    color: black;
    font-size: 15px;
  }
}
</style>
